package com.tbsoft.ehr.dao.user;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.tbsoft.ehr.dao.BaseDaoImpl;
import com.tbsoft.ehr.login.MenuInfo;
import com.tbsoft.ehr.login.RoleInfo;
import com.tbsoft.ehr.login.UserInfo;
import com.tbsoft.ehr.model.Resume;
/**
 * @desc 角色管理
 * @author vidy
 *
 */
public class RoleDaoImpl extends BaseDaoImpl<RoleInfo>
implements RoleDao{

	@Override
	public int getRoleCount(final RoleInfo role) {
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				StringBuffer sb = new StringBuffer(
						"from RoleInfo ri where ri.status !='D'");
				if (role.getRoleId() != null && !role.getRoleId().isEmpty()) {
					sb.append(" and ri.roleId like '%").append(
							role.getRoleId()).append("%'");
				}
				if (role.getRoleName() != null && !role.getRoleName().isEmpty()) {
					sb.append(" and ri.roleName like'%").append(
							role.getRoleName()).append("%'");
				}
				Query query = session.createQuery(sb.toString());
				return query.list();

			}
		});
		return list.size();
	}

	@Override
	public List<RoleInfo> getRoleList(final RoleInfo role, final int startIndex,final int count) {
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				StringBuffer sb = new StringBuffer(
						"from RoleInfo ri where ri.status !='D'");
				if (role.getRoleId() != null && !role.getRoleId().isEmpty()) {
					sb.append(" and ri.roleId like '%").append(
							role.getRoleId()).append("%'");
				}
				if (role.getRoleName() != null && !role.getRoleName().isEmpty()) {
					sb.append(" and ri.roleName like'%").append(
							role.getRoleName()).append("%'");
				}
				Query query = session.createQuery(sb.toString());
				query.setFirstResult(startIndex).setMaxResults(count);
				return query.list();

			}
		});
		return list;
	}
	@Override
	public RoleInfo getRoleById(String id) {
		return (RoleInfo) getHibernateTemplate().get(RoleInfo.class, id);
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<MenuInfo> getMenuListByLvl(final int lvl) {
		
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = session.createCriteria(MenuInfo.class);
				// (Criteria) DetachedCriteria.forClass(Resume.class);
				criteria.add(Restrictions.ne("status", "D"));
				criteria.add(Restrictions.eq("level", lvl));
				return criteria.list();
			}
		});
		return list;
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<MenuInfo> getMenuListByIdList(final List<String> idList) {
		List list = getHibernateTemplate().executeFind(new HibernateCallback(){
			
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				String strIds = idList.toString().replaceAll("\\[|\\]|\\s", "");
				String hql = "from MenuInfo mi where mi.menuId in ("+strIds+") and mi.status != 'D'";
				return session.createQuery(hql).list();
			}});
		return list;
	}
}
